package com.google.code.swingonfire.component.calendar;

import java.awt.Color;
import java.awt.Font;
import java.util.Calendar;

import javax.swing.table.AbstractTableModel;

public class JYearTableModel extends AbstractTableModel
{

    public static class MonthEntry
    {
        private final JCalendarModel model;
        private final Calendar calendar;

        public MonthEntry(JCalendarModel model, Calendar calendar)
        {
            super();

            this.model = model;
            this.calendar = calendar;
        }

        public JCalendarModel getModel()
        {
            return model;
        }

        public Calendar getCalendar()
        {
            return calendar;
        }

        public String getMonthName()
        {
            return model.getMonthName(calendar);
        }

        public Font getMonthFont(Font defaultFont)
        {
            return model.getMonthFont(calendar, defaultFont);
        }

        public Color getMonthForeground(Color defaultColor)
        {
            return model.getMonthForeground(calendar, defaultColor);
        }
    }

    private static final long serialVersionUID = -8017978959983892906L;

    private final MonthEntry[][] monthEntries = new MonthEntry[3][4];

    public JYearTableModel(JCalendarModel model, int year)
    {
        super();

        Calendar current = (Calendar) model.getDisplayed().clone();

        current.set(Calendar.DAY_OF_MONTH, 1);
        current.set(Calendar.MONTH, Calendar.JANUARY);
        current.set(Calendar.YEAR, year);

        for (int y = 0; y < 4; y += 1)
        {
            for (int x = 0; x < 3; x += 1)
            {
                monthEntries[x][y] = new MonthEntry(model, (Calendar) current.clone());
                current.add(Calendar.MONTH, 1);
            }
        }
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public int getRowCount()
    {
        return 4;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public int getColumnCount()
    {
        return 3;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public Object getValueAt(int rowIndex, int columnIndex)
    {
        return monthEntries[columnIndex][rowIndex];
    }

}
